<?php

namespace App\Admin\Controllers;

use App\Town;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;

class TownController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('村居管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('村居管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('村居管理');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Town::class, function (Grid $grid) {

            $grid->exporter(new ExcelExpoter($grid,
                'towns-'.date('Y-m-d', time()),
                ['name'],
                [],
                ['村居名称']
            ));
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like('name', '村居名称');
                // $filter->between('start_date', '开始日期')->date();
                // $filter->between('end_date', '结束日期')->date();
                // $filter->between('birth', '出生')->date();
                // $filter->equal('state','状态')->select($state_msg);
            });
            //$grid->id('ID')->sortable();
            $grid->column('name','村居名称');
//            $grid->created_at();
//            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Town::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '村居名称');
//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
        });
    }
}
